package org.davidtrevien.com.imput.omi.panels.center.planning;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;

import layout.TableLayout;

import org.davidtrevien.com.imput.omi.panels.center.planning.datenavigation.DateNavigationCtrl;
import org.davidtrevien.com.imput.omi.panels.center.planning.details.TaskDetailsCtrl;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TOs;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TOsDaysModel;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TOsTasksModel;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TTask;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.TreeTableModel;

@SuppressWarnings("serial")
public class PlanningView
extends JPanel {
	
	protected JXTreeTable tasktable;
	
	protected TreeTableModel taskmodel;
	
	protected TreeTableModel daymodel;
	
	protected DefaultMutableTreeTableNode rootTask;
	
	protected DefaultMutableTreeTableNode rootDay;
	
	protected JScrollPane treeTaskScrollpane;
	
	protected JScrollPane treeDayScrollpane;

	protected JXTreeTable daytable;

	protected JSplitPane splitpaneH;

	protected JSplitPane splitpaneV;
	
	protected TaskDetailsCtrl taskDetailsCtrl;
	
	protected DateNavigationCtrl dateNavigationCtrl;

	protected Font font = new Font("Lucida",Font.PLAIN, 10);
	
	public PlanningView(final TableCellRenderer dayTableRenderer) {
		initComponent(dayTableRenderer);
	}

	private void initComponent(final TableCellRenderer dayTableRenderer) {
		double size[][] = new double[][] {
				{TableLayout.FILL, TableLayout.PREFERRED},
				{25,TableLayout.FILL}
		};
		TableLayout layout = new TableLayout(size);
		
		setLayout(layout);
		
		this.dateNavigationCtrl = new DateNavigationCtrl();
		add(this.dateNavigationCtrl.getView(), "1,0");
		
		this.rootTask = new DefaultMutableTreeTableNode();
		this.rootDay = new DefaultMutableTreeTableNode();
		this.taskmodel = new TOsTasksModel(this.rootTask);
		this.daymodel = new TOsDaysModel(this.rootDay);
		this.tasktable = new JXTreeTable(this.taskmodel);
		this.daytable = new JXTreeTable(this.daymodel) {
			@Override
			public TableCellRenderer getCellRenderer(int row, int column) {
				return dayTableRenderer;
			}
		};
		
		this.taskDetailsCtrl = new TaskDetailsCtrl();
		
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
			@Override
			public java.awt.Component getTreeCellRendererComponent(
					javax.swing.JTree tree, Object value, boolean sel,
					boolean expanded, boolean leaf, int row,
					boolean hasFocus) {
				super.getTreeCellRendererComponent(
						tree, value, sel, expanded, leaf, row, hasFocus);
				if (value instanceof DefaultMutableTreeTableNode) {
					DefaultMutableTreeTableNode node = (DefaultMutableTreeTableNode) value;
					String text = "";
					if (node.getUserObject() instanceof TOs) {
						TOs tos = (TOs) node.getUserObject();
						text = tos.getName();
					}
					else if (node.getUserObject() instanceof TTask) {
						TTask ttask = (TTask) node.getUserObject();
						text = ttask.getNumberTCR();
					}
					
					setText(text);
					setFont(PlanningView.this.font);
					setBackgroundNonSelectionColor(new Color(0x00000000,true));
					setOpaque(false);
					
					if (node.isLeaf()
							&& node.getParent() == tree.getModel().getRoot()) {
						setIcon(getDefaultClosedIcon());
					}
				}
				
				return this;
			};
			
		};
		this.tasktable.setTreeCellRenderer(renderer);
		this.daytable.setTreeCellRenderer(renderer);

		this.tasktable.setFont(this.font);
		this.tasktable.getTableHeader().setFont(this.font);
		this.tasktable.setIntercellSpacing(new Dimension(0, 0));
		this.daytable.setFont(this.font);
		this.daytable.getTableHeader().setFont(this.font);
		this.daytable.setIntercellSpacing(new Dimension(0, 0));

		this.tasktable.getColumnModel().getColumn(0).setPreferredWidth(150);
		this.tasktable.getColumnModel().getColumn(1).setPreferredWidth(120);
		this.tasktable.getColumnModel().getColumn(2).setPreferredWidth(50);
		this.tasktable.getColumnModel().getColumn(3).setPreferredWidth(50);
		this.tasktable.getColumnModel().getColumn(4).setPreferredWidth(50);
		this.tasktable.getColumnModel().getColumn(5).setPreferredWidth(50);
		
		this.daytable.getColumnExt(0).setMinWidth(0);
		this.daytable.getColumnExt(0).setMaxWidth(0);

		
		int dayWidth = 40;
		for (int i = 1; i < this.daytable.getColumnCount(); i++) {
			this.daytable.getColumnModel().getColumn(i).setPreferredWidth(dayWidth);
		}
		
		this.treeTaskScrollpane = new JScrollPane(this.tasktable);
		this.treeDayScrollpane = new JScrollPane(this.daytable);
		this.tasktable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.daytable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		this.splitpaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		this.splitpaneH.setDividerLocation(552);
		this.splitpaneH.setLeftComponent(this.treeTaskScrollpane);
		this.splitpaneH.setRightComponent(this.treeDayScrollpane);
		
		this.splitpaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		this.splitpaneV.setDividerLocation(600);
		this.splitpaneV.setTopComponent(this.splitpaneH);
		this.splitpaneV.setBottomComponent(this.taskDetailsCtrl.getView());
		
		add(this.splitpaneV, "0,1 1,1");
	}

	public DefaultMutableTreeTableNode getRoot() {
		return this.rootTask;
	}
}
